﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Web.Views.Admin.Controls.News
{
    public partial class NewsEditControl : System.Web.UI.UserControl
    {
        [System.ComponentModel.DefaultValue(true)]
        public bool AutoReturn
        {
            get
            {
                object o = ViewState["AutoReturn"];
                if (o == null)
                    return true;
                return ((bool)(o));
            }
            set
            {
                ViewState["AutoReturn"] = value;
            }
        }

        [System.ComponentModel.DefaultValue(DetailsViewMode.ReadOnly)]
        public DetailsViewMode DefaultMode
        {
            get
            {
                return DetailsView1.DefaultMode;
            }
            set
            {
                DetailsView1.DefaultMode = value;
            }
        }

        protected string ReturnUrl
        {
            get
            {
                return ((string)(ViewState["ReturnUrl"]));
            }
            set
            {
                ViewState["ReturnUrl"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(IsPostBack))
                ReturnUrl = Request.UrlReferrer.PathAndQuery;
        }

        private void RedirectToReturnUrl()
        {
            if (!(String.IsNullOrEmpty(ReturnUrl)) && AutoReturn)
                Response.Redirect(ReturnUrl);
            else
                Response.Redirect("list.aspx");
        }

        protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                e.KeepInEditMode = true;
                ErrorControl1.Text = e.Exception.Message;
            }
            else
                RedirectToReturnUrl();
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
                ErrorControl1.Text = e.Exception.Message;
            }
            else
                RedirectToReturnUrl();
        }

        protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                ErrorControl1.Text = e.Exception.Message;
            }
            else
                RedirectToReturnUrl();
        }

        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if ((e.CommandName == "Cancel") && (DetailsView1.DefaultMode == DetailsViewMode.Insert))
                RedirectToReturnUrl();
        }

        protected void BackToList_Click(object sender, EventArgs e)
        {
            RedirectToReturnUrl();
        }

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            DropDownList ddlcity = (DropDownList)DetailsView1.FindControl("ddlAreaCity");
                if(ddlcity!=null)
                {
                    string citykey = ddlcity.SelectedValue;
                    SqlDataSource1.InsertParameters["AreaID"].DefaultValue = citykey;
                }
        }
    }
}